﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using ShakeIt_.ServiceReference2;
using System.ComponentModel;

namespace ShakeIt_
{
    public partial class fansPage : PhoneApplicationPage
    {
        private List<_User> fansList;
        public List<_User> FansList
        {
            set
            {
                if (fansList != value)
                {
                    fansList = value;
                    NotifyPropertyChanged("FansList");
                }
            }
            get
            {
                return fansList;
            }
        }
        public fansPage()
        {
            InitializeComponent();
            /* 粉丝名字列表 Data.fansList (类型：List<_User>)
             * 点击粉丝名，执行以下3步
             * 1.Data.proxy.IsFocusAsync( <粉丝>.id )
             * 2.Data.userFocusedId = <粉丝>.id
             * 3.Data.userFocusedName = <粉丝>.username
             * 4.Data.userFocusedPhone = <粉丝>.cellPhone
             * 5.进入 userInfoPage.xaml
             */
            Data.proxy.GetFansCompleted += new EventHandler<GetFansCompletedEventArgs>(GetFansCompleted);
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            Data.proxy.GetFansAsync(Data.userId);

            FansList = null;
            fansBox.ItemsSource = null;

            FansList = Data.fansList;
            fansBox.ItemsSource = FansList;

            if (FansList.Count == 0)
            {
                textBlock1.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                textBlock1.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        private void TextBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            foreach (_User fans in FansList)
            {
                if(fans.username == (sender as TextBlock).Text)
                {
                    Data.proxy.IsFocusAsync(fans.id, Data.userId);
                    Data.userFocusedId = fans.id;
                    Data.userFocusedName = fans.username;
                    Data.userFocusedPhone = fans.cellPhone;
                    this.NavigationService.Navigate(new Uri("/userInfoPage.xaml", UriKind.Relative));
                }
            }
        }

        private void GetFansCompleted(object sender, GetFansCompletedEventArgs e)
        {
            FansList = null;
            fansBox.ItemsSource = null;

            FansList = Data.fansList;
            fansBox.ItemsSource = FansList;

            if (FansList.Count == 0)
            {
                textBlock1.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                textBlock1.Visibility = System.Windows.Visibility.Collapsed;
            }
        }
    }

    public partial class fansPage : INotifyPropertyChanged
    {
        #region Property Changed

        private void NotifyPropertyChanged(string propertyName)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        #endregion
    }
}